package com.gdssoft.pdaweb.action.deviceReg;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.JsonUtil;
import com.gdssoft.core.web.action.BaseAction;
import com.gdssoft.pdaweb.model.deviceReg.DeviceReg;
import com.gdssoft.pdaweb.service.deviceReg.DeviceRegService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import flexjson.JSONSerializer;

public class DeviceRegAction extends BaseAction {
	@Resource
	private DeviceRegService deviceRegService;

	private DeviceReg deviceReg;

	private Long deviceId;

	public DeviceReg getDeviceReg() {
		return this.deviceReg;
	}

	public void setDeviceReg(DeviceReg deviceReg) {
		this.deviceReg = deviceReg;
	}

	public Long getDeviceId() {
		return this.deviceId;
	}

	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}

	/**
	 * 显示列表
	 */
	public String list() {

		QueryFilter filter = new QueryFilter(getRequest());
		List<DeviceReg> list = deviceRegService.getAll(filter);

		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
				.append(filter.getPagingBean().getTotalItems()).append(
						",result:");

		JSONSerializer json = JsonUtil.getJSONSerializer();
		buff.append(json.exclude(new String[] { "class", "deviceReg" })
				.serialize(list));
		buff.append("}");

		jsonString = buff.toString();

		return SUCCESS;
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 */
	public String multiDel() {

		String[] ids = getRequest().getParameterValues("ids");
		if (ids != null) {
			for (String id : ids) {
				try {
					deviceRegService.remove(new Long(id));
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		jsonString = "{success:true}";
		return SUCCESS;
	}

	/**
	 * 显示详细信息
	 * 
	 * @return
	 */
	public String get() {
		deviceId = Long
				.parseLong(getRequest().getParameterValues("deviceId")[0]);
		DeviceReg deviceReg = deviceRegService.get(deviceId);
		// Gson gson = new
		// GsonBuilder().excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();
		Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
				.create();
		// 将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(deviceReg));
		sb.append("}");

		setJsonString(sb.toString());
		return SUCCESS;
	}

	/**
	 * 添加及保存操作
	 */
	public String save() {
		String un = ContextUtil.getCurrentUser().getUsername();
		if (deviceReg.getId() == null) {
			try {
				this.deviceReg.setCreateddt(new Date());
				this.deviceReg.setCreatedby(un);
				this.deviceRegService.save(this.deviceReg);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		} else {
			DeviceReg orgdeviceReg = deviceRegService.get(deviceReg.getId());
			try {
				BeanUtil.copyNotNullProperties(orgdeviceReg, deviceReg);
				orgdeviceReg.setLasteditdt(new Date());
				orgdeviceReg.setLasteditby(un);
				deviceRegService.save(orgdeviceReg);
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		setJsonString("{success:true}");

		return SUCCESS;
	}

	/**
	 * 检查mac是否存在
	 */
	public String check() {
		String mac = getRequest().getParameterValues("mac")[0];

		StringBuffer sb = new StringBuffer();
		if (deviceRegService.checkExists(mac)){// mac存在时
			sb.append("{success:true,flag:1}");
		} else {
			sb.append("{success:true,flag:0}");
		}
		// if(deviceRegService.checkActivity(mac)){//设备可以使用时
		// 		sb.append("{success:true,flag:0}");
		// } else {
		// 		sb.append("{success:true,flag:2}");
		// }
		setJsonString(sb.toString());
		return SUCCESS;
	}

}
